﻿using System;
using System.Collections.Generic;
using System.Data.SqlServerCe;
using System.Diagnostics;

namespace Flare.DB
{
    class report_free_ip
    {
        public List<string> Index(string networks = "172.24.0.")
        {
            var free_ip_list = new List<string>();
            var temp_free_ip = new List<int>();
            using (SqlCeConnection connection = new SqlCeConnection(glob.DBConnection))
            {
                using (SqlCeCommand command = new SqlCeCommand(@"SELECT [net_ip] FROM [endpoints] where net_ip like @networks", connection))
                {
                    command.Parameters.AddWithValue("@networks", "%" + networks + "%");
                    try
                    {
                        connection.Open();
                    }
                    catch (Exception ex)
                    {
                        glob.kernel.Panic(glob.PanicType.db_connect, ex);
                    }
                    try
                    {
                        using (SqlCeDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                temp_free_ip.Add(Convert.ToInt32(reader["net_ip"].ToString().Split('.')[3]));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(command.CommandText);
                        glob.kernel.Panic(glob.PanicType.sql, ex);
                    }
                }
            }

            int? start = null;
            int? stop = null;
            for (int i = 1; i < 255; i++)
            {
                if (temp_free_ip.Contains(i) == false)
                {
                    if (start.HasValue == false)
                    {
                        start = i;
                    }
                    if (i == 254)
                    {
                        stop = 254;
                    }
                }
                else
                {
                    if (stop.HasValue == false && start.HasValue)
                    {
                        stop = i - 1;
                    }
                }

                if (start.HasValue && stop.HasValue)
                {
                    if (start == stop)
                    {
                        free_ip_list.Add(string.Format("172.24.0.{0}", start));
                    }
                    else
                    {
                        free_ip_list.Add(string.Format("172.24.0.{0} - 172.24.0.{1}", start, stop));
                    }
                    start = null; stop = null;
                }

            }
            return free_ip_list;
        }

        public int Count(string networks = "172.24.0.")
        {
            using (SqlCeConnection connection = new SqlCeConnection(glob.DBConnection))
            {
                using (SqlCeCommand command = new SqlCeCommand(@"SELECT COUNT(*) FROM [endpoints] where net_ip like @networks", connection))
                {
                    command.Parameters.AddWithValue("@networks", "%" + networks + "%");
                    try
                    {
                        connection.Open();
                    }
                    catch (Exception ex)
                    {
                        glob.kernel.Panic(glob.PanicType.db_connect, ex);
                    }

                    int count = 0;
                    try
                    {
                        using (SqlCeDataReader reader = command.ExecuteReader())
                        {
                            reader.Read();
                            count = 254 - Convert.ToInt16(reader[0]);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(command.CommandText);
                        glob.kernel.Panic(glob.PanicType.sql, ex);
                    }
                    return count;
                }
            }
        }
    }
}
